package com.mayikt.controller;

import com.mayikt.req.EbookQueryReq;
import com.mayikt.req.EbookSaveReq;
import com.mayikt.resp.CommonResp;
import com.mayikt.resp.EbookQueryResp;
import com.mayikt.resp.PageResp;
import com.mayikt.service.EbookService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/ebook")
public class EbookController {
    @Resource
    private EbookService ebookService;

    @GetMapping("/list")
    public CommonResp list(EbookQueryReq ebookQueryReq) {
        CommonResp<PageResp<EbookQueryResp>> resp = new CommonResp<>();
        PageResp<EbookQueryResp> pageResp = ebookService.list(ebookQueryReq);
        resp.setData(pageResp);
        return resp;
    }

    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody EbookSaveReq ebookSaveReq) {
        CommonResp resp = new CommonResp<>();
        int result = ebookService.save(ebookSaveReq);
        if(result < 1) {
            resp.setSuccess(false);
        }
        return resp;
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        int result = ebookService.delete(id);
        if(result < 1) {
            resp.setSuccess(false);
        }
        return resp;
    }
}
